#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/9

import time
import threading


def addNum():
    global num  # 定义全局变量

    print("----get  num:", num)
    time.sleep(1)

    lock.acquire()  # 加锁
    num -= 1
    lock.release()  # 释放锁


lock = threading.Lock()  # 得到锁
num = 100

thread_list = []

for i in range(100):
    t = threading.Thread(target=addNum)
    t.start()
    thread_list.append(t)

for i in thread_list:
    i.join()

print("final num:", num)
